package com.thinkgem.jeesite.common.im.message.server;

import org.springframework.stereotype.Component;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;

/**
 * 
 * @description: 聊天消息服务启动类
 * @fileName:IMServer.java
 * @createTime:2015年7月23日 下午7:33:54
 * @author:肖震
 * @version 1.0.0
 * 
 */
@Component
public class IMServer{
	
	// 端口
	private int port;
	
	// 构造方法
	public IMServer(int port){
		this.port = port;
	}

	// 服务运行方法
	public void run()throws Exception{
		EventLoopGroup bossGroup = new NioEventLoopGroup(); 
		EventLoopGroup workerGroup = new NioEventLoopGroup();
		try{
			ServerBootstrap b = new ServerBootstrap(); 
			b.group(bossGroup, workerGroup)
					.channel(NioServerSocketChannel.class) 
					.childHandler(new IMServerInitializer())
					.option(ChannelOption.SO_BACKLOG, 128) 
					.childOption(ChannelOption.SO_KEEPALIVE, true); 
			
			System.out.println("imServer 启动了,端口：" + port);
			b.bind(port);		
		}catch (Exception e){
			e.printStackTrace();
		}
	}
	
	// web方式运行启动
//	public static void main(String[] args)throws Exception{
//		int port;
//		if (args.length > 0){
//			port = Integer.parseInt(args[0]);
//		}else{
//			port = 8080;
//		}
//		new IMServer(port).run();
//	}

}
